import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/views/index'
import Home from '@/views/index/index'
import { Four0Four } from '@daelui/vdog/dist/business/business.umd.min.js'
import config from '@/micros/config'

// 判断环境是否是微应用打开
let microPath = ''
if (window.__POWERED_BY_QIANKUN__) {
  microPath = config.microPath
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: microPath + '/',
      name: 'index',
      component: Index,
      redirect: to => {
        return microPath + '/home'
      },
      children: [
        // 首页
        {
          path: 'home',
          name: 'home',
          component: Home
        },
        // 笔记
        {
          path: 'note',
          name: 'note',
          component: () => import(/* webpackChunkName: "note" */ '@/views/note'),
          redirect: to => {
            return microPath + '/note/list'
          },
          children: [
            {
              path: 'list',
              name: 'note-list',
              component: () => import(/* webpackChunkName: "note" */ '@/views/note/list')
            },
            {
              path: 'item/:id?',
              name: 'note-item',
              component: () => import(/* webpackChunkName: "note" */ '@/views/note/item')
            },
            {
              path: 'edit/:id?',
              name: 'note-edit',
              component: () => import(/* webpackChunkName: "note" */ '@/views/note/edit')
            },
            {
              path: 'type',
              name: 'note-type',
              component: () => import(/* webpackChunkName: "note" */'@/views/note/list-type')
            }
          ]
        },
        // 友情链接
        {
          path: 'links',
          name: 'links',
          component: () => import(/* webpackChunkName: "links" */ '@/views/links'),
          redirect: to => {
            return microPath + '/links/list'
          },
          children: [
            {
              path: 'list',
              name: 'links-list',
              component: () => import(/* webpackChunkName: "links" */ '@/views/links/list')
            },
            {
              path: 'edit/:id?',
              name: 'links-edit',
              component: () => import(/* webpackChunkName: "links" */ '@/views/links/edit')
            }
          ]
        },
        // 字典
        {
          path: 'dictionary',
          name: 'dictionary',
          component: () => import(/* webpackChunkName: "dictionary" */ '@/views/dictionary/index.vue'),
          redirect: to => {
            return microPath + '/dictionary/list'
          },
          children: [
            {
              path: 'list',
              name: 'dictionary-list',
              component: () => import(/* webpackChunkName: "dictionary" */ '@/views/dictionary/list.vue')
            },
            {
              path: 'item/:id?',
              name: 'dictionary-item',
              component: () => import(/* webpackChunkName: "dictionary" */ '@/views/dictionary/item.vue')
            },
            {
              path: 'edit/:id?',
              name: 'dictionary-edit',
              component: () => import(/* webpackChunkName: "dictionary" */ '@/views/dictionary/edit.vue')
            }
          ]
        }
      ]
    },
    {
      path: microPath + '/404',
      name: '404',
      component: Four0Four,
      props: { home: '/home' }
    }
  ]
})

// beforeEach每次进行路由跳转时都会执行
router.beforeEach((to, from, next) => {
  // if (to.matched.length === 0) {
  //   // from.path ? next({name: from.name}) : next('/404')
  //   next(microPath + '/404')
  // } else {
  //   next()
  // }
  next()
})

export default router
